/*
 * Copyright 2009 Yannick Stucki (yannickstucki.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yannickstucki.android.musicqueue;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * The song adapter is the logic behind any list where songs are displayed.
 * 
 * @author Yannick Stucki (yannickstucki@gmail.com)
 * 
 */
public class SongAdapter extends ArrayAdapter<Song> { //TODO: implements SectionIndexer

  /**
   * Needed for various operations.
   */
  private Context context;

  /**
   * Needed for adding/removing starred songs.
   */
  private SongManager songManager;

  /**
   * The constructor sets the necessary references and forwards the songs and
   * layout to the super class constructor.
   */
  public SongAdapter(Context context, SongManager songManager, Song[] songs) {
    super(context, R.layout.row, R.id.label, songs);
    this.context = context;
    this.songManager = songManager;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    Song song = this.getItem(position);

    TextView text;

    if (convertView == null) {
      convertView = View.inflate(context, R.layout.row, null);
    }
    // TODO: this is only a temporary fix for no songs.
    if (song == null) {
      return convertView;
    }
    text = (TextView) convertView.findViewById(R.id.label);

    ImageView star;
    star = (ImageView) convertView.findViewById(R.id.star);
    if (position == songManager.hideSongIndex) {
      text.setText("");
      star.setVisibility(View.INVISIBLE);
    } else {
      text.setText(song.displayStrings[Song.currentDisplayStringIndex]);
      star.setVisibility(View.VISIBLE);

      if (song.starred) {
        star.setImageResource(R.drawable.starred);
      } else {
        star.setImageResource(R.drawable.unstarred);
      }

      star.setTag(song);
      star.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
          Song song = (Song) v.getTag();
          song.starred = !song.starred;
          ImageView star2 = (ImageView) v;
          if (song.starred) {
            star2.setImageResource(R.drawable.starred);
            songManager.addStarredSong(song);
          } else {
            star2.setImageResource(R.drawable.unstarred);
            songManager.removeStarredSong(song);
          }
        }
      });
    }
    return convertView;
  }
}
